<?php

class PageController extends Zend_Controller_Action{
	
	public function createAction(){
		require "/../library/CMS/Content/Item/Page.php";
		$pageFrom = new Form_PageForm();
		$pageFrom->setAction('create');
		$pageFrom->setMethod('post');
		if($this->getRequest()->isPost()){
			if($pageFrom->isValid($_POST)){
				$itemPage = new CMS_Content_Item_Page();
				$itemPage->name = $pageFrom->getValue('name');
				$itemPage->headline = $pageFrom->getValue('headline');
				$itemPage->description = $pageFrom->getValue('description');
				$itemPage->content = $pageFrom->getValue('content');
				
				/*if($pageFrom->image->isUploaded()){ //To ensure this is exiting!!
					$pageFrom->image->receive(); //To upload it to server!
					$itemPage->image = 'images/upload'.basename($pageFrom->image->getFileName());
				}*/

				$itemPage->save();
				return  $this->_forward('list');
			}
		}
		$this->view->form = $pageFrom;
	}
	
	public function listAction(){
		$id = $this->_request->getParam('id');
		echo $this->id;
		$pageModel = new Model_Page();
		$select = $pageModel->select();
		$select->order('name');
		$currentPages = $pageModel->fetchAll($select);
		if($currentPages->count() > 0){
			$this->view->pages = $currentPages;
		}else{
			$this->view->pages = null;
		}
	}
	public function editAction(){
		$id = $this->_request->getParam('id');
		$itemPage = new CMS_Content_Item_Page($id);
		$pageForm = new Form_PageForm();
		$pageForm->setAction('edit');
		if($this->getRequest()->isPost()){
			if($pageForm->isValid($_POST)){
				$itemPage->name = $pageForm->getValue('name');
				$itemPage->headline = $pageForm->getValue('headline');
				$itemPage->description = $pageForm->getValue('description');
				$itemPage->content = $pageForm->getValue('content');
				/*if($pageForm->image->isUploaded()){
					print_r("success!");
					$pageForm->image->receive();
					$itemPage->image = 'images/upload'.basename($pageForm->image->getFileName());
				}*/
				$itemPage->save();
				return  $this->_forward('list');
			}
		}
		$pageForm->populate($itemPage->toArray());
		/*
		$imagePreview = $pageForm->createElement('image','image_preview');
		$imagePreview->setLabel('Preview Image: ');
		$imagePreview->setAttrib('style','width: 200px; height:auto;');
		$imagePreview->setOrder(4);
		$imagePreview->setImage($itemPage->image);
		$pageForm->addElement($imagePreview);
		*/
		$this->view->form = $pageForm;
	}
	
	public function deleteAction(){
		require "/../library/CMS/Content/Item/Abstract.php";
		$id = $this->_request->getParam('id');
		$itemPage = new CMS_Content_Item_Page($id);		
		$itemPage->delete($id);
		return $this->_forward('list');
	}
	public function openAction(){
		$id=$this->_request->getParam('id');
		//first confirm the page exit
		$pageModel=new Model_Page();
		$nodesModel= new Model_ContentNode();
		if(!$pageModel->find($id)->current()){
			//the error handel will catch this exception:
			throw new Zend_Controller_Action_Exception(
			"The page you requested was not found",404);
		}else{
			$page=new CMS_Content_Item_Page($id);
			
			$temp=$pageModel->find($id)->current();
			$nodes=$nodesModel->find($id)->current();
			$page->id=$temp['id'];
			$page->name=$temp['name'];
			$page->content=$nodes['content'];
			$page->headline=$nodes['headline'];
			
			$this->view->page=$page;
		}
	}
}